package com.sbl.bajw.pc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.model.ApartmentModel;
import com.sbl.bajw.pc.service.ApartmentService;
import com.sbl.bajw.util.JsonUtil;

@Controller
@RequestMapping("/Apartment")
public class ApartmentAction {
	
	@Resource
	private ApartmentService service;
	
	@RequestMapping(value="/getModel")
	public @ResponseBody String getModel(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		
		HashMap<String, Object> model = service.getModel(id);
		JSONObject jobj = JSONObject.fromObject(model);
		String result = jobj.toString();
		return result;
	}
	
	@RequestMapping(value="/getZdryGrid")
	public @ResponseBody String getZdryGrid(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		id = RequestUnit.getString("id");
		String result = service.getZdryGrid(id);
		return result;
	}
	
	@RequestMapping(value="/getModelByOrgs")
	public @ResponseBody String getModelByOrgs(@RequestParam(value="orgid",required=false) String orgid,
			@RequestParam(value="maxx",required=false) String maxx,
			@RequestParam(value="maxy",required=false) String maxy,
			@RequestParam(value="minx",required=false) String minx,
			@RequestParam(value="miny",required=false) String miny,
			@RequestParam(value="jwtag",required=false) String jwtag) throws Exception{
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(orgid != null && !orgid.equals("") && !"zddw".equals(jwtag)){
			parm.put("orgid", orgid+"%");
		}
		
		parm.put("jwtag", jwtag);
		parm.put("maxx", maxx);
		parm.put("maxy", maxy);
		parm.put("minx", minx);
		parm.put("miny", miny);
		if(orgid != null && !orgid.equals("")){
			orgid = orgid + "%";
		}
		List<HashMap<String, Object>> models ;
		if("zdry".equals(jwtag)){
			models = service.getZdryModelsByOrgId(parm);
		}
		else{
			models = service.getModelsByOrg(parm);
		}
		
		String result = JsonUtil.ConvertArrayToJson(models);
		return result;
	}
	
	@RequestMapping(value="/getModelByOrgGrid")
	public @ResponseBody String getModelByOrgGrid(@RequestParam(value="orgid",required=false) String orgid,
			@RequestParam(value = "type", required = false) String type) throws Exception{
		HashMap<String, Object> parm = new HashMap<String, Object>();
		String models = null;
		if(orgid != null && !orgid.equals("")){
			parm.put("orgid", orgid + "%");
		}
		if("其他".equals(type)){
			models = service.getApartmentByOrgListPage(parm);
		} else if ("all".equals(type)){
			models = service.getAllModelsByOrgGridListPage(parm);
		} else if("物业式小区".equals(type) || "城中村小区".equals(type)){
			parm.put("mainType", type);
			models = service.getApartmentByConsListPage(parm);
		} else {
			parm.put("buildingType", type);
			models = service.getApartmentByConsListPage(parm);
		}
		return models;
	}
	
	@RequestMapping(value = "/getAllModel")
	public @ResponseBody String getAllModel(HttpServletRequest request, HttpServletResponse response){
		List<ApartmentModel> models = service.getAllApartmentModel();
		JSONArray json = JSONArray.fromObject(models);
		String result = json.toString();
		return result;
	}
	
	@RequestMapping(value="/getCenterCount")
	public @ResponseBody String getCenterCount(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String orgid = request.getParameter("orgid");
		String level = request.getParameter("level");
		String jwtag = request.getParameter("jwtag");
		
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(orgid != null && !orgid.equals("") && !"zddw".equals(jwtag)){
			parm.put("orgid", orgid+"%");
		}
		parm.put("level", level);
		parm.put("jwtag", jwtag);
		List<Map<String, Object>> results;
		if("zddw".equals(jwtag)){
			results = service.getPACJHApartment(parm);
		}
		else{
			if(level.equals("6"))
			{
				results = service.getCenterCountByXiaoqu(parm);
			}
			else{
				results = service.getCenterCount(parm);
			}
		}
		
		String result = JsonUtil.ConvertArrayToJson(results);
		return result;
	}
	
	@RequestMapping(value="/StatisticsByOrgid")
	public @ResponseBody String StatisticsByOrgid(@RequestParam(value="orgid",required=false) String orgid,
			@RequestParam(value="isXiaoqu",required=false) String isXiaoqu) throws Exception{
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(orgid != null && !orgid.equals("")){
			parm.put("orgid", orgid+"%");
		}
		List<Map<String, Object>> results;
		if(isXiaoqu != null && !isXiaoqu.equals("")){
			results = service.StatisticsByXiaoquid(parm);
		}
		else{
			results = service.StatisticsByOrgid(parm);
		}
		
		String result =  JsonUtil.ConvertArrayToJson(results);
		return result;
	}
	
	@RequestMapping(value = "/getMainTypeCount")
	public @ResponseBody String getMainTypeCount(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = new HashMap<String, Object>();
		String orgid = request.getParameter("orgid");
		if(orgid != null && orgid.equals("")){
			parm.put("orgid", orgid);
		}
		List<Map<String, Object>> list = service.getMainTypeCount(parm);
		String json = JsonUtil.ConvertArrayToJson(list);
		return json;
	}
	
	@RequestMapping(value = "/getCountByBuildingType")
	public @ResponseBody String getCountByBuildingType(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = new HashMap<String, Object>();
		String orgid = request.getParameter("orgid");
		if(orgid != null && orgid.equals("")){
			parm.put("orgid", orgid);
		}
		List<Map<String, Object>> list = service.getCountByBuildingType(parm);
		String json = JsonUtil.ConvertArrayToJson(list);
		return json;
	}
	
	@RequestMapping(value = "/getAll")
	public @ResponseBody String getAll(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = new HashMap<String, Object>();
		String orgid = request.getParameter("orgid");
		parm.put("orgid", orgid + "%");
		List<Map<String, Object>> list = service.getAll(parm);
		String json = JsonUtil.ConvertArrayToJson(list);
		return json;
	}
	
	@RequestMapping(value = "/getModelByGridConditions")
	public @ResponseBody String getModelByGridConditions(HttpServletRequest request, HttpServletResponse response){
		String dz = request.getParameter("dz");
		String ldbm = request.getParameter("ldbm");
		String isqbcz = request.getParameter("isqbcz");
		String isdt = request.getParameter("isdt");
		String ishhcz = request.getParameter("ishhcz");
		String ismj = request.getParameter("ismj");
		String fullid = request.getParameter("fullid");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(dz != null && "".equals(dz)){
			parm.put("dz", "%" + dz + "%");
		}
		if(ldbm != null && "".equals(ldbm)){
			parm.put("ldbm", "%" + ldbm + "%");
		}
		parm.put("isqbcz", isqbcz);
		parm.put("isdt", isdt);
		parm.put("ishhcz", ishhcz);
		parm.put("ismj", ismj);
		parm.put("fullid", fullid + "%");
		String json = service.getModelByGridConditions(parm);
		return json;
	}
	
	@RequestMapping(value = "/details")
	public ModelAndView details(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		HashMap<String, Object> model = service.getModel(id);
		request.setAttribute("model", model);
		ModelAndView mav = new ModelAndView("/view/grid/chuzuwu_grid");
		return mav;
	}
	
	@RequestMapping(value = "/getCountByFullid")
	public @ResponseBody String getCountByFullid(HttpServletRequest request, HttpServletResponse response){
		String fullid = request.getParameter("fullid");
		String level = (fullid.split("-").length + 1) + "";
		HashMap<String, Object> parm = new HashMap<String, Object>();
		parm.put("fullid", fullid + "%");
		parm.put("level", level);
		List<Map<String, Object>> list = service.getCountByFullid(parm);
		String json = JsonUtil.ConvertArrayToJson(list);
		return json;
	}
}
